# Load necessary libraries
library(tidyverse)
library(readr)
library(ggplot2)
library(maps)

# Create World Map


# Read the world CSV file
world_data <- read_csv("PJP-WORLD-MAP.CSV")

# Rename columns for easier access
colnames(world_data) <- c("Country", "Count")

# Get world map data
world_map <- map_data("world") %>%
  mutate(region = tolower(region))

# Prepare data for plotting
world_data <- world_data %>%
  mutate(Country = tolower(Country)) %>%
  rename(region = Country)


# Check for mismatches
mismatches <- setdiff(world_data$region, world_map$region)
print(mismatches)

# Correct mismatches if necessary
world_data <- world_data %>%
  mutate(region = case_when(
    region == "united states of america" ~ "usa",
    region == "venezuela, bolivarian republic of..." ~ "venezuela",
    region == "united kingdom of great britain and northern ireland" ~ "uk",
    TRUE ~ region
  ))

# Merge map data with participant data
world_map_data <- left_join(world_map, world_data, by = "region") %>%
  mutate(Count = replace_na(Count, 0))

# Define color thresholds and corresponding colors
world_color_thresholds <- c(0, 1, 10, 50, 100)
world_colors <- c("grey", "lightblue", "blue", "green", "red")

# Convert 'Count' to factor based on thresholds
world_map_data <- world_map_data %>%
  mutate(Count = cut(Count, breaks = c(-Inf, 0, 10, 50, 100, Inf), labels = c("0", "1-10", "10-50", "50-100", ">100")))

# Create color-coded world map using discrete colors
world_color_map <- ggplot(world_map_data, aes(long, lat, group = group, fill = Count)) +
  geom_polygon(color = "black") +
  scale_fill_manual(values = world_colors,
                    na.value = "grey") +
  theme_minimal() +
  labs(title = "PJP Participants by Country",
       fill = "Participants")

# Save color-coded world map
# ggsave("world_color_map.png", plot = world_color_map, width = 12, height = 8)

# Define greyscale colors corresponding to thresholds
world_greyscale_colors <- c("white", "grey80", "grey60", "grey40", "black")

# Create greyscale world map using discrete colors
world_greyscale_map <- ggplot(world_map_data, aes(long, lat, group = group, fill = Count)) +
  geom_polygon(color = "black") +
  scale_fill_manual(values = world_greyscale_colors, na.value = "white") +
  theme_minimal() +
  labs(title = "PJP Participants by Country (Greyscale)", fill = "Participants") +
  theme(axis.title = element_blank(),  # Remove axis titles
        axis.text = element_blank(),   # Remove axis text
        axis.ticks = element_blank(),  # Remove axis ticks
        panel.grid = element_blank())  # Remove gridlines

# Save greyscale world map
# Figure 6
ggsave("world_greyscale_map.png", plot = world_greyscale_map, width = 12, height = 8)


# Create US Map

# Read the CSV file, skipping the first row
data <- read_csv("pjp-us-map.csv", skip = 1)

# Rename columns for easier access
colnames(data) <- c("State", "Count")

# Get US map data
us_states <- map_data("state")

# Prepare data for plotting
data <- data %>%
  mutate(State = tolower(State)) %>%
  rename(region = State)

# Merge map data with participant data
us_map_data <- left_join(us_states, data, by = "region")

# Define color thresholds
color_thresholds <- c(1, 10, 50, 100, 200, 300)

# Create color-coded map
color_map <- ggplot(us_map_data, aes(long, lat, group = group, fill = Count)) +
  geom_polygon(color = "black") +
  scale_fill_gradientn(colors = c("white", "yellow", "orange", "red", "darkred"),
                       breaks = color_thresholds,
                       labels = color_thresholds) +
  theme_minimal() +
  labs(title = "US-Based PJP Participants by State",
       fill = "Participants")

# Save color-coded map
# ggsave("us_color_map.png", plot = color_map, width = 10, height = 6)

# Create greyscale map
greyscale_map <- ggplot(us_map_data, aes(long, lat, group = group, fill = Count)) +
  geom_polygon(color = "black") +
  scale_fill_gradient(low = "white", high = "black",
                      breaks = color_thresholds,
                      labels = color_thresholds) +
  theme_minimal() +
  labs(title = "US-Based PJP Participants by State (Greyscale)",
       fill = "Participants")

# Save greyscale map
# Figure 6 
ggsave("us_greyscale_map.png", plot = greyscale_map, width = 10, height = 6)
